package com.blast.manager.net.presenter

import android.content.Context
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import com.blast.manager.blast.BlastActivity
import com.blast.manager.net.NetCheckActivity
import com.wxl.common.life.ViewModelQuick
import com.wxl.common.viewmodel.AbsPresenter

abstract class AbsNetCheckPresenter : AbsPresenter() {

    open lateinit var rootView: RelativeLayout
    open lateinit var context: Context
    open var childView: View? = null

    override fun onCreated(vararg args: Any) {
        rootView = args[0] as RelativeLayout
        context = rootView.context
        onNetCheckCreated(*args)
    }

    fun show(vararg args: Any) {
        rootView.removeAllViews()
        childView = View.inflate(context, getLayoutResId(), null)
        childView!!.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)
        rootView.addView(childView,childView!!.layoutParams)
        showView(*args)
    }

    fun dismiss() {
        rootView.removeView(childView)
    }


    fun getActivity(): NetCheckActivity {
        return context as NetCheckActivity
    }

    fun getBlastActivity(): BlastActivity {
        return context as BlastActivity
    }

    fun <P : AbsNetCheckPresenter> get(cls: Class<P>): P {
        return ViewModelQuick.get(getActivity(), cls)
    }

    internal abstract fun showView(vararg args: Any)

    open fun onNetCheckCreated(vararg args: Any) {}

    abstract fun getLayoutResId(): Int

    open fun onKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
        return false
    }

    internal fun isAdd() : Boolean{
        return  childView != null && childView!!.isShown && childView!!.parent != null
    }
}